// pages/recharge/recharge.js
var app = getApp()
Page({
    /**
     * 页面的初始数据
     */
    data: {
        active: 0,
        isTicket: false,
        ticket: '',
        hd: null,
        rechargeArr: [],
        balance: '',
        url:null,
    },
    pickHd: function(e) {
        console.log('pickHD-----------',e)
        var hd = e.currentTarget.dataset.hd
        this.setData({
            hd: hd,
            ticket:null
        })
    },
    getBalance: function() {
        var _this = this
        wx.showToast({
            title: 'loading...',
            icon: 'loading',
            duration: 60000
        })
        wx.request({
            url: app.globalData.url+'/v1/payment/show',
            method: 'GET',
            data: {
                openid: app.globalData.openId
            },
            success: function(r) {
                r = r.data
                if (r.code == 0) {
                    wx.hideToast()
                    _this.setData({
                        balance: r.data.money
                    })
                } else {
                    wx.hideToast()
                    wx.showToast({
                        title: r.data.message,
                        icon: 'none',
                        duration: 2000
                    })
                }
            }
        })
    },
    isTicketChange: function(e) {
        this.setData({
            isTicket: e.detail.value,
            ticket: '',
        })
    },
	//输入框获取焦点
	inputFocus:function(e){
		console.log('输入框获取焦点')
		this.setData({
		    hd:{id:null}
		})
	},
    ticketInput: function(e) {
        this.setData({
            ticket: e.detail.value,
            hd:{id:null}
        })
        console.log(this.data.ticket)
    },
    bindPickerChange: function(e) {
        console.log(e)
        this.setData({
            index: e.detail.value
        })
    },
    getRechargeList: function() {
        var _this = this
        wx.request({
            url: app.globalData.url+'/v1/recharge/show',
            method: "GET",
            data: {
                pagesize: 1000,
                current: 1
            },
            success: function(r) {
                r = r.data
                if (r.code == 0) {
                    _this.setData({
                        rechargeArr: r.data.items,
                        hd: r.data.items[0]
                    })
                } else {
                    wx.showToast({
                        title: r.data.message,
                        icon: 'none'
                    })
                }
            }
        })
    },
    /*
     *序列号兑换
    */
    ticketRecharge: function() {
        var _this = this
        wx.showToast({
            title: 'loading...',
            icon: 'loading',
            duration: 60000
        })
        if(_this.data.hd.id && !_this.data.ticket){
            //微信充值
            console.log('微信充值------')
            wx.request({
                url: app.globalData.url+'/v1/recharge/recharge',
                method: 'POST',
                header: {
                    'content-type': 'application/x-www-form-urlencoded'
                },
                data: {
                    recharge_id: _this.data.hd.id,
                    openid: app.globalData.openId
                },
                success: function(r) {
                    r = r.data
                    if (r.code == 0) {
                        _this.beforePay(r.data)
                    } else {
                        wx.hideToast()
                        wx.showToast({
                            title: r.data.message,
                            icon: 'none'
                        })
                    }
                }
            })
        }else{
            //兑换
            if(_this.data.ticket.substring(0,1)>=0 && _this.data.ticket.substring(0,1)<=9){
                console.log('随机金额-------------')
                _this.setData({
                    url : app.globalData.url+'/v1/payment/pcode'
                })
                _this.getCode()
            }else{
                console.log('固定金额-------------')
                _this.setData({
                    url:app.globalData.url+'/v1/payment/code'
                })
                _this.getCode()
            }
        }
        
    },
    getCode(){
        const _this = this
        wx.request({
            url: _this.data.url,
            method: 'POST',
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            data: {
                code: _this.data.ticket,
                openid: app.globalData.openId
            },
            success: function(r) {
                r = r.data
                if (r.code == 0) {
                    wx.hideToast()
                    wx.showToast({
                        title: '兑换成功'
                    })
                    setTimeout(function() {
                        wx.navigateBack()
                    }, 1500)
                } else {
                    wx.hideToast()
                    wx.showToast({
                        title: r.data.message,
                        icon: 'none'
                    })
                }
            }
        })
    },
    //确定充值
    moneyRecharge: function() {
        var _this = this
        wx.showToast({
            title: 'loading...',
            icon: 'loading',
            duration: 60000
        })
        wx.request({
            url: app.globalData.url+'/v1/recharge/recharge',
            method: 'POST',
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            data: {
                recharge_id: _this.data.hd.id,
                openid: app.globalData.openId
            },
            success: function(r) {
                r = r.data
                if (r.code == 0) {
                    _this.beforePay(r.data)
                } else {
                    wx.hideToast()
                    wx.showToast({
                        title: r.data.message,
                        icon: 'none'
                    })
                }
            }
        })
    },
    beforePay: function(data) {
        var _this = this
        wx.request({
            url: app.globalData.url+'/v1/pay/pay',
            method: 'POST',
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            data: {
                id: data.id,
                orderName: 'WOWSURPRISE有惊喜',
                openid: app.globalData.openId
            },
            success: function(r) {
                r = r.data
                if (r.code == 0) {
                    _this.pay(r.data.package)
                } else {
                    wx.hideToast()
                    wx.showToast({
                        title: r.data.message,
                        icon: 'none'
                    })
                }
            }
        })
    },
    pay: function(data) {
        var _this = this
        wx.requestPayment({
            timeStamp: data.timeStamp,
            nonceStr: data.nonceStr,
            package: data.package,
            signType: 'MD5',
            paySign: data.paySign,
            success(res) {
                wx.hideToast()
                wx.showToast({
                    title: '充值成功！',
                })
                setTimeout(function() {
                    wx.navigateBack()
                }, 1500)
            },
            fail(res) {
                wx.hideToast()
                wx.showToast({
                    icon: 'none',
                    title: '用户取消支付',
                })
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.getRechargeList()
        this.getBalance()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})
